package com.sctvcloud.zhaohua_sd.ui.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.ArrayMap;

import com.jiongbull.jlog.JLog;
import com.mob.tools.utils.DeviceHelper;
import com.sctvcloud.zhaohua_sd.application.Constances;
import com.sctvcloud.zhaohua_sd.base.BaseActivity;
import com.sctvcloud.zhaohua_sd.beans.NewsItem;
import com.sctvcloud.zhaohua_sd.beans.SingleResult;
import com.sctvcloud.zhaohua_sd.http.AbsNetCallBack;
import com.sctvcloud.zhaohua_sd.http.NetUtils;
import com.sctvcloud.zhaohua_sd.ui.activities.CountyNewsDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.H5NormalActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.LivingActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.NewesDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.TopicDetailActivity;
import com.sctvcloud.zhaohua_sd.ui.activities.VideoPlayActivity;
import com.sctvcloud.zhaohua_sd.utils.UserManager;

import java.util.HashMap;

/**
 * Created by liuyu on 2017/8/21.
 * <p>
 * 进入新闻详情、H5等页面的工具管理类
 */

public class SkipUtil {

    public static final int REQUEST_CODE_PLAY_BASE = 121;
    public static final String EX_POS = VideoPlayActivity.EXTRA_POS;
    public static final String EX_POS_ITEM = VideoPlayActivity.EXTRA_POS_ITEM;

    /**
     * 跳转到详情页（新闻、专题、直播、H5等）
     *
     * @param context
     * @param item
     * @param tag
     * @return
     */
    public static boolean skipWithNewsItem(@NonNull Context context, NewsItem item, HashMap<String, String> tag) {
        if (item == null || !item.isNewsTypeValued()) {
            return false;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                //新闻详情
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                //专题详情
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                //直播详情
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                //H5
                intent = new Intent(context, H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                intent.putExtra(H5NormalActivity.EX_TITLE, item.getNewsTitle());
                break;
            case Constances.NEWS_TYPE_COUNTY:
                //区县新闻详情
                intent = new Intent(context, CountyNewsDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
        }
        if (intent != null) {
            intent.putExtra("TAG", tag);
            if (item.isLuzhouShow()) {
                intent.putExtra(Constances.IS_CITY_STATE, true);
            }
            context.startActivity(intent);
            return true;
        }
        return false;
    }

    /**
     * 跳转到详情页（新闻、专题、直播、H5等）
     *
     * @param context
     * @param item
     * @param tag
     * @return
     */
    public static Intent getIntentWithNewsItem(@NonNull Context context, NewsItem item, HashMap<String, String> tag) {
        if (item == null || !item.isNewsTypeValued()) {
            return null;
        }
        Intent intent = null;
        switch (item.getNewsType()) {
            case Constances.NEWS_TYPE_GRAPHIC:
            case Constances.NEWS_TYPE_AUDIO:
            case Constances.NEWS_TYPE_VIDEO:
                //新闻详情
                intent = new Intent(context, NewesDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_TOPIC:
                //专题详情
                intent = new Intent(context, TopicDetailActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                break;
            case Constances.NEWS_TYPE_LIVE:
                //直播详情
                intent = new Intent(context, LivingActivity.class);
                intent.putExtra(BaseActivity.EX_URL, item.getNewsUrl());
                intent.putExtra(BaseActivity.EX_DATA, item.getNewsImage());
                break;
            case Constances.NEWS_TYPE_H5:
                //H5
                intent = new Intent(context, H5NormalActivity.class);
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                intent.putExtra(H5NormalActivity.EX_TITLE, item.getNewsTitle());
                break;
            case Constances.NEWS_TYPE_COUNTY:
                //区县新闻详情
                intent = new Intent(context, CountyNewsDetailActivity.class);
                intent.putExtra(H5NormalActivity.EX_HTML, item.getNewsUrl());
                break;
        }
        if (intent != null) {
            intent.putExtra("TAG", tag);
//			context.startActivity(intent);
            return intent;
        }
        return null;
    }

    /**
     * 跳转到视频播放页
     *
     * @param ac
     * @param url
     * @param playedPos
     * @param itemPos
     */
    public static void skipToVideoPlay(Activity ac, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (ac == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(ac, VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        ac.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }

    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     */
    public static void skipToVideoPlay(Fragment fg, String url, int playedPos, int itemPos) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg.getContext(), VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }


    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     * @param coverImgUrl
     */
    public static void skipToVideoPlayWithCover(Fragment fg, String url, int playedPos, int itemPos,String coverImgUrl) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg.getContext(), VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        intent.putExtra(VideoPlayActivity.EXTRA_COVER, coverImgUrl);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }


    /**
     * 跳转到视频播放页
     *
     * @param fg
     * @param url
     * @param playedPos
     * @param itemPos
     * @param coverImgUrl
     */
    public static void skipToVideoPlayWithCover(Activity fg, String url, int playedPos, int itemPos,String coverImgUrl) {
//        ac.startActivityForResult();
        if (fg == null || TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(fg, VideoPlayActivity.class);
        intent.putExtra(VideoPlayActivity.EX_URL, url);
        intent.putExtra(VideoPlayActivity.EXTRA_POS, playedPos);
        intent.putExtra(VideoPlayActivity.EXTRA_POS_ITEM, itemPos);
        intent.putExtra(VideoPlayActivity.EXTRA_COVER, coverImgUrl);
        fg.startActivityForResult(intent, REQUEST_CODE_PLAY_BASE);
    }




    /**
     * 上传统计 日志
     *
     * @param ctx
     * @param owner
     * @param //siteNumber  站点四川观察为1
     * @param resurceType 来源，1直播观看、2新闻阅读，12投稿、14分享等
     * @param typeName    1直播，2主播圈，3投稿，4视频新闻，5音频新闻，6图文新闻
     * @param decription  描述。例如登陆成功
     * @param newsId      新闻id没有不传
     * @param nodeId      栏目ID，例如，专题，高层 等，传id
     */
    public static void skipToAddUserOperate(Context ctx, final String owner,  final int resurceType, final int typeName, final String decription,  final String newsId, final String nodeId) {
        String userPhone = "";
        if (UserManager.isLoginS()) {
            userPhone = UserManager.getInstance().getUser().getPhoneNumber();
        }
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("siteNumber", Constances.SITE_ID);
        p.put("resurceType", resurceType);
        if(typeName>0) {
            p.put("typeName", typeName);
        }
        p.put("decription", decription);
        if(!TextUtils.isEmpty(userPhone)) {
            p.put("cellPhone", userPhone);
        }
        if(!TextUtils.isEmpty(newsId)) {
            p.put("newsId", newsId);
        }
        if(!TextUtils.isEmpty(nodeId)) {
            p.put("nodeId", nodeId);
        }
        p.put("device", DeviceHelper.getInstance(ctx).getDeviceId());
        JLog.e("skipToAddUserOperate--", owner+"  resurceType="+resurceType + " typeName=" + typeName + "  cellPhone=" + userPhone + "  decription=" +decription+"  newsId="+ newsId + "  nodeId=" + nodeId+" Device="+DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOperate(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("skipToAddUserOperate 成功--", owner+" -- "+resurceType + " " + typeName +  " " + newsId + " " + nodeId+"  decription=" +decription );
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("skipToAddUserOperate 失败--", owner+" -- "+resurceType + " " + typeName + " "  + newsId + " " + nodeId);
            }

            @Override
            public void onEnd() {
                super.onEnd();
            }
        });
    }

    /**
     * 上传统计 日志
     *
     * @param ctx
     * @param owner
     * @param //siteNumber  站点四川观察为1
     * @param resurceType 来源，1直播观看、2新闻阅读，12投稿、14分享等
     * @param typeName    1直播，2主播圈，3投稿，4视频新闻，5音频新闻，6图文新闻
     * @param decription  描述。例如登陆成功
     * @param newsId      新闻id没有不传
     * @param nodeId      栏目ID，例如，专题，高层 等，传id
     * @param phoneNumber 用户手机号
     */
    public static void skipToAddUserOperate(Context ctx, final String owner, final int resurceType, final int typeName, final String decription,  final String newsId, final String nodeId,final String phoneNumber) {

        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("siteNumber", Constances.SITE_ID);
        p.put("resurceType", resurceType);
        if(typeName>0) {
            p.put("typeName", typeName);
        }
        p.put("decription", decription);
        if(!TextUtils.isEmpty(phoneNumber)) {
            p.put("cellPhone", phoneNumber);
        }
        if(!TextUtils.isEmpty(newsId)) {
            p.put("newsId", newsId);
        }
        if(!TextUtils.isEmpty(nodeId)) {
            p.put("nodeId", nodeId);
        }
        p.put("device", DeviceHelper.getInstance(ctx).getDeviceId());
        JLog.e("skipToAddUserOperate--", owner+"  resurceType="+resurceType + " typeName=" + typeName + "  cellPhone=" + phoneNumber + "  decription=" +decription+"  newsId="+ newsId + "  nodeId=" + nodeId+" Device="+DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOperate(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("skipToAddUserOperate 成功--", owner+" -- "+resurceType + " " + typeName +  " " + newsId + " " + nodeId+"  decription=" +decription );
            }

            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("skipToAddUserOperate 失败--", owner+" -- "+resurceType + " " + typeName + " "  + newsId + " " + nodeId);
            }

            @Override
            public void onEnd() {
                super.onEnd();
            }
        });
    }
    /**
     * 发送在线统计
     * @param ctx
     * @param owner
     * @param id 直播、频道、广播ID
     * @param liveType 1 直播 ，2 频道 3 广播
     */
    public static void skipToAddOnline(Context ctx, String owner,final String id,final int liveType){
        String userPhone = "";
        if (UserManager.isLoginS()) {
            userPhone = UserManager.getInstance().getUser().getPhoneNumber();
        }
        ArrayMap<String, Object> p = new ArrayMap<>();
        p.put("liveId", id);
        p.put("userId", userPhone);
        p.put("type", liveType);
        p.put("deviceId", DeviceHelper.getInstance(ctx).getDeviceId());
        NetUtils.getNetAdapter().addUserOnline(owner, p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult info) {
                JLog.e("上传 在线统计日志 成功-- id = "+id+"  --type="+liveType);
            }
            @Override
            public void onError(Throwable ex, String errorMsg, int netCode) {
                super.onError(ex, errorMsg, netCode);
                JLog.e("上传 在线统计日志 失败-- id = "+id+"  --type="+liveType);
            }

            @Override
            public void onEnd() {
                super.onEnd();

            }
        });
    }

    /**
     * 上传APP安装信息
     * @param ctx
     * @param owner
     */
    public static void sendInstall(Context ctx, String owner) {
        PackageManager pm = ctx.getPackageManager();
        PackageInfo pi = null;
        String versionName = "";
        try {
            pi = pm.getPackageInfo(ctx.getPackageName(), 0);
            versionName = pi.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String deviceId =  DeviceHelper.getInstance(ctx).getDeviceId();
        NetUtils.getNetAdapter().sendInstallData(owner, deviceId,versionName, new AbsNetCallBack<SingleResult>(SingleResult.class) {
            @Override
            public void onSuc(SingleResult result) {
                JLog.e("---sendInstall---onSuc--");
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                super.onError(ex, errorMsg);
                JLog.e("---sendInstall---onError--");
            }
        });

    }
}
